cmake_minimum_required(VERSION 2.8)

PROJECT(libyar C)

set (CMAKE_MODULE_PATH ${libyar_SOURCE_DIR}/../CMake)

file(READ ${libyar_SOURCE_DIR}/src/include/libyar/version.h LIBYAR_VERSION_H_CONTENTS)
string(REGEX MATCH "LIBYAR_VERSION_FLAVOUR[ \t]+\"([A-Z]+)\"" LIBYAR_VERSION_FL ${LIBYAR_VERSION_H_CONTENTS})
string(REGEX MATCH "(CURRENT|STABLE|ALPHA|BETA|PRERELEASE|RELEASE)" LIBYAR_VERSION_FL ${LIBYAR_VERSION_FL})
string(REGEX MATCH "LIBYAR_VERSION_MAJOR[ \t]+([0-9]+)" LIBYAR_VERSION_MJ ${LIBYAR_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBYAR_VERSION_MJ ${LIBYAR_VERSION_MJ})
string(REGEX MATCH "LIBYAR_VERSION_MINOR[ \t]+([0-9]+)" LIBYAR_VERSION_MI ${LIBYAR_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBYAR_VERSION_MI ${LIBYAR_VERSION_MI})
string(REGEX MATCH "LIBYAR_VERSION_PATCH[ \t]+([0-9]+)" LIBYAR_VERSION_PT ${LIBYAR_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBYAR_VERSION_PT ${LIBYAR_VERSION_PT})
string(REGEX MATCH "LIBYAR_VERSION_SUFFX[ \t]+([0-9]+)" LIBYAR_VERSION_SX ${LIBYAR_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBYAR_VERSION_SX ${LIBYAR_VERSION_SX})

string(COMPARE EQUAL ${LIBYAR_VERSION_FL} "RELEASE" LIBYAR_RELEASE)
if(LIBYAR_RELEASE)
	set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}.${LIBYAR_VERSION_PT})
	set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI})
else(LIBYAR_RELEASE)
	string(COMPARE EQUAL ${LIBYAR_VERSION_FL} "PRERELEASE" LIBYAR_PRERELEASE)
	if(LIBYAR_PRERELEASE)
		set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}.${LIBYAR_VERSION_PT}-rc${LIBYAR_VERSION_SX})
		set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}-rc${LIBYAR_VERSION_SX})
	else(LIBYAR_PRERELEASE)
		string(COMPARE EQUAL ${LIBYAR_VERSION_FL} "BETA" LIBYAR_BETA)
		if(LIBYAR_BETA)
			set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}.${LIBYAR_VERSION_PT}-b${LIBYAR_VERSION_SX})
			set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}-b${LIBYAR_VERSION_SX})
		else(LIBYAR_BETA)
			string(COMPARE EQUAL ${LIBYAR_VERSION_FL} "ALPHA" LIBYAR_ALPHA)
			if(LIBYAR_ALPHA)
				set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}.${LIBYAR_VERSION_PT}-a${LIBYAR_VERSION_SX})
				set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}-a${LIBYAR_VERSION_SX})
			else(LIBYAR_ALPHA)
				string(COMPARE EQUAL ${LIBYAR_VERSION_FL} "STABLE" LIBYAR_STABLE)
				if(LIBYAR_STABLE)
					set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}-STABLE)
					set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.${LIBYAR_VERSION_MI}-STABLE)
				else(LIBYAR_STABLE)
					set(LIBYAR_VERSION ${LIBYAR_VERSION_MJ}.0-CURRENT)
					set(LIBYAR_LIBVERSION ${LIBYAR_VERSION_MJ}.0-CURRENT)
				endif(LIBYAR_STABLE)
			endif(LIBYAR_ALPHA)
		endif(LIBYAR_BETA)
	endif(LIBYAR_PRERELEASE)
endif(LIBYAR_RELEASE)

set(PACKAGE "libyar")
set(PACKAGE_NAME ${PACKAGE})
set(PACKAGE_VERSION ${LIBYAR_VERSION})

set(LIBYAR_LIBS "")
set(CMAKE_C_FLAGS "-std=c99 -pedantic -Wall -Wextra -pipe -O3 -fno-strict-aliasing")
set(CMAKE_REQUIRED_FLAGS ${CMAKE_ANSI_CFLAGS})
set(CMAKE_REQUIRED_DEFINITIONS -D_FILE_OFFSET_BITS=64)
set(CMAKE_REQUIRED_LIBRARIES ${LIBYAR_LIBS})

add_subdirectory(src)
add_subdirectory(doc)